GETOPT

Section: User Commands (1)
Updated: LOCAL
Index Return to Main Contents
 

NAME

getopt - format flags for shell scripts  

SYNOPSIS

getopt flag_spec argument ...  

DESCRIPTION

Getopt is a program intended to be called by scripts to ``canonicalize'' their arguments before processing them, just as the getopt(3) routine does for C programs. (This need for scripts is usually most noticeable in the way lint(1) handles the -n flag.)

The following two examples provide the initial parsing for a script which takes two flags, -a and -b, the second of which takes an argument.

# For /bin/csh...
set argv = (`getopt "ab:" $*`)
if ( $status ) then
    echo "Read the documentation and try again." >/dev/tty
    exit 1
endif
set Aflag=0
set Name=NONE
while "$1" != "--"
    switch ("$1")
        case '-a':
            set Aflag=1
            breaksw
        case '-b':
            shift
            set Name=$1
            breaksw
    endsw
    shift
end
shift
echo Aflag=$Aflag / Name=$Name / Remaining args are $*

# For /bin/sh...
set -- `getopt "d:s" $@`
if test $? != 0 ; then
    echo "Read the documentation and try again."
    exit 1
fi
Aflag=0
Name=NONE
for f
do
    case "$f" in
        -a) Aflag=1
            ;;
        -b) shift
            Name=$2
            ;;
        --) break
            ;;
    esac
    shift
done
shift
echo Aflag=$Aflag / Name=$Name / Remaining args are $*
 

DIAGNOSTICS

The program burps the standard getopt(3) diagnostics to standard error, and exits with a non-zero status if an error occurs. It is arguable wrong that the diagnostics imply that the program is named ``getopt'' rather than the real name of the script. It is undeniably AT&T-compatible to do this, however.  

SEE ALSO

csh(1), sh(1), getopt(3)  

AUTHOR

Rich $alz
Mirror Systems
(mirror!rs, rs@mirror.TMC.COM)


 

Index

NAME
SYNOPSIS
DESCRIPTION
DIAGNOSTICS
SEE ALSO
AUTHOR

This document was created by man2html, using the manual pages.
Time: 22:49:12 GMT, December 11, 2024